package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.CategoryBO;
import com.cskaoyan.bean.bo.CategoryDeleteBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.vo.CategoryL1VO;
import com.cskaoyan.bean.vo.CategoryListVOWithChild;
import com.cskaoyan.bean.vo.CategoryVO;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author by WANGJun
 * @date 2022/6/5 15:07
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("l1")
    public BaseRespVo l1() {
        TVO<CategoryL1VO> categoryL1VOTVO = categoryService.l1();
        return BaseRespVo.ok(categoryL1VOTVO);
    }

    @GetMapping("list")
    public BaseRespVo list(BaseParam baseParam) {
        TVO<CategoryListVOWithChild> categoryVOS = categoryService.listCategory(baseParam);
        return BaseRespVo.ok(categoryVOS);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody CategoryBO categoryBO) {
        if (categoryBO.getPid() == 0 && "L2".equals(categoryBO.getLevel())) {
            return BaseRespVo.invalidParameter("二级类目必须选择父类目");
        }
        categoryBO.setAddTime(new Date());
        CategoryVO categoryVO = categoryService.createCategory(categoryBO);
        return BaseRespVo.ok(categoryVO);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody CategoryBO categoryBO) {
        if (categoryBO.getPid() == null && "L2".equals(categoryBO.getLevel())) {
            return BaseRespVo.invalidParameter("参数不对");
        }
        categoryBO.setUpdateTime(new Date());
        categoryService.updateCategory(categoryBO);
        return BaseRespVo.ok(null);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryDeleteBO categoryDeleteBO) {
        categoryService.deleteCategory(categoryDeleteBO.getId());
        return BaseRespVo.ok(null);
    }
}
